package com.example.riceexambackend.service.impl;

import com.example.riceexambackend.common.errorcode.ServiceErrorCodeConstants;
import com.example.riceexambackend.common.exception.ServiceException;
import com.example.riceexambackend.controller.param.question.QuestionAddParam;
import com.example.riceexambackend.dao.dataobject.QuestionDO;
import com.example.riceexambackend.dao.dataobject.QuestionTypeDO;
import com.example.riceexambackend.dao.dataobject.UserDO;
import com.example.riceexambackend.dao.mapper.QuestionMapper;
import com.example.riceexambackend.dao.mapper.QuestionTypeMapper;
import com.example.riceexambackend.dao.mapper.UserMapper;
import com.example.riceexambackend.service.QuestionService;
import com.example.riceexambackend.service.dto.question.QuestionAddDTO;
import com.example.riceexambackend.service.dto.question.QuestionSelectDTO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private QuestionTypeMapper questionTypeMapper;


    @Override
    public List<QuestionSelectDTO> selectAllQuestion() {
        // 1. 先查询出题目列表
        List<QuestionDO> questionDOList = questionMapper.selectList();
        // 2. 二次查询出上传用户和关联题型
        for (QuestionDO questionDO : questionDOList) {
            Long uploadUserId = questionDO.getUploadUserId();
            UserDO uploadUser = userMapper.selectById(uploadUserId);
            questionDO.setUploadUser(uploadUser);
            Long questionTypeId = questionDO.getQuestionTypeId();
            QuestionTypeDO questionType = questionTypeMapper.selectById(questionTypeId);
            questionDO.setQuestionType(questionType);
        }
        // 3. 数据脱敏返回
        for (QuestionDO questionDO : questionDOList) {
            questionDO.setUploadUser(getMaskUser(questionDO.getUploadUser()));
            questionDO.setQuestionType(getMaskQuestionType(questionDO.getQuestionType()));
        }
        // 4. 封装DTO对象返回
        List<QuestionSelectDTO> questionSelectDTOList = new ArrayList<>();
        for (QuestionDO questionDO : questionDOList) {
            QuestionSelectDTO questionSelectDTO = new QuestionSelectDTO();
            questionSelectDTO.setId(questionDO.getId());
            questionSelectDTO.setQuestionName(questionDO.getQuestionName());
            questionSelectDTO.setOptionA(questionDO.getOptionA());
            questionSelectDTO.setOptionB(questionDO.getOptionB());
            questionSelectDTO.setOptionC(questionDO.getOptionC());
            questionSelectDTO.setOptionD(questionDO.getOptionD());
            questionSelectDTO.setQuestionStatus(questionDO.getQuestionStatus());
            questionSelectDTO.setRightAnswer(questionDO.getRightAnswer());
            questionSelectDTO.setUploadUser(questionDO.getUploadUser());
            questionSelectDTO.setQuestionType(questionDO.getQuestionType());
            questionSelectDTOList.add(questionSelectDTO);
        }
        return questionSelectDTOList;
    }

    private UserDO getMaskUser(UserDO srcUserDO) {
        UserDO retDO = new UserDO();
        retDO.setId(srcUserDO.getId());
        retDO.setUserName(srcUserDO.getUserName());
        retDO.setUserAccount(srcUserDO.getUserAccount());
        return retDO;
    }

    private QuestionTypeDO getMaskQuestionType(QuestionTypeDO srcQuestionTypeDO) {
        QuestionTypeDO retDO = new QuestionTypeDO();
        retDO.setId(srcQuestionTypeDO.getId());
        retDO.setQuestionName(srcQuestionTypeDO.getQuestionName());
        retDO.setQuestionScore(srcQuestionTypeDO.getQuestionScore());
        return retDO;
    }

    @Override
    public QuestionAddDTO insertQuestion(QuestionAddParam param) {
        // 1. 进行参数校验
        String questionName = param.getQuestionName().trim();
        Long uploadUserId = param.getUploadUserId();
        Long questionTypeId = param.getQuestionTypeId();
        if (StringUtils.isBlank(questionName) ||
            uploadUserId == null ||
            questionTypeId == null) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_ADD_PARAM_NULL);
        }
        // 2. 具体参数校验
        if (questionName.length() > 256) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_ADD_PARAM_ERROR);
        }
        UserDO dbUserDO = userMapper.selectById(uploadUserId);
        if (dbUserDO == null) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_ADD_PARAM_ERROR);
        }
        QuestionTypeDO dbQuestionTypeDO = questionTypeMapper.selectById(questionTypeId);
        if (dbQuestionTypeDO == null) {
            throw new ServiceException(ServiceErrorCodeConstants.QUESTION_ADD_PARAM_ERROR);
        }
        // 3. 插入到数据库中
        QuestionDO questionDO = new QuestionDO();
        questionDO.setQuestionName(questionName);
        questionDO.setOptionA(param.getOptionA());
        questionDO.setOptionB(param.getOptionB());
        questionDO.setOptionC(param.getOptionC());
        questionDO.setOptionD(param.getOptionD());
        questionDO.setRightAnswer(param.getRightAnswer());
        questionDO.setQuestionTypeId(param.getQuestionTypeId());
        questionDO.setUploadUserId(param.getUploadUserId());
        questionMapper.insertQuestion(questionDO);
        // 4. 返回响应
        QuestionAddDTO questionAddDTO = new QuestionAddDTO();
        questionAddDTO.setQuestionId(questionDO.getId());
        return questionAddDTO;
    }
}
